package com.example.draganddraw;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.graphics.Shader;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;



public class Myview extends View {
    private Path path1 = new Path();
    private Path path2 = new Path();
    private Path path3 = new Path();
    private Path path4 = new Path();
    private Path path5 = new Path();
    private Path path6 = new Path();
    public Myview(Context context) {
        super(context);
    }

    public Myview(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public Myview(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }
    private LinearGradient mShader =new LinearGradient(0f,0f,40f,60f,
            new int[]{Color.RED,Color.GREEN,Color.BLUE,Color.YELLOW},
            null, Shader.TileMode.REPEAT);
//    定义画笔
    private RectF rect = new RectF();
    private Paint paint = new Paint();
//  重写该方法，进行绘图

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
//        将画布绘制成白色
        canvas.drawColor(Color.WHITE);
//        去锯齿
        paint.setAntiAlias(true);
        paint.setShader(mShader);//为画笔设置渐变器
        paint.setShadowLayer(25f,20f,20f,Color.GRAY);
        paint.setColor(Color.BLUE);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(4f);
        int viewWidth = this.getWidth();
//        绘制圆形
        canvas.drawCircle(viewWidth/10+10,viewWidth/10+10,viewWidth/10,paint);
//        绘制正方形
        canvas.drawRect(10f,viewWidth/5+20,viewWidth/5+10,viewWidth*2/5+20,paint);
//        绘制矩形
        canvas.drawRect(10f,viewWidth*2/5+30,viewWidth/5+10,viewWidth/2+30,paint);
        rect.set(10f,viewWidth/2+40,10+viewWidth/5,viewWidth*3/5+40);
//        绘制圆角矩形
        canvas.drawRoundRect(rect,15f,15f,paint);
        rect.set(10f,viewWidth*3/5+50,10+viewWidth/5,viewWidth*7/10+50);
//        绘制椭圆
        canvas.drawOval(rect,paint);
//        使用一个Path对象封闭成一个五边形
        path1.moveTo(10f,viewWidth*9/10+60);
        path1.lineTo(viewWidth/5+10,viewWidth*9/10+60);
        path1.lineTo(viewWidth/10+10,viewWidth*7/10+60);
        path1.close();
//        根据路径进行绘制，绘制三角形
        canvas.drawPath(path1,paint);
//        使用一个path对象封闭一个五边形
        path2.moveTo(10+viewWidth/15,viewWidth*9/10+70);
        path2.lineTo(10+viewWidth*2/15,viewWidth*9/10+70);
        path2.lineTo(10+viewWidth/5,viewWidth+70);
        path2.lineTo(10+viewWidth/10,viewWidth*11/10+70);
        path2.lineTo(10f,viewWidth+70);
        path2.close();
        canvas.drawPath(path2,paint);
    }
//
}
